﻿@inject IEventService _eventService
@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@implements IDisposable

@attribute [StreamRendering(true)]

@if (Model != null)
{
    <link rel="stylesheet" href="_content/CnGalWebSite.Shared/css/share/almanac.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="@("https://fonts.googleapis.com/css2?family=Oswald:wght@400;700&family=Passion+One:wght@400;700;900&display=swap")" rel="stylesheet">


    <CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="@($"{Model.Name} - {Model.Year}")" />

    <CnGalWebSite.Shared.MasaComponent.Shared.Components.Magazine.MasaMagazineLayout LayoutId="almanac-magazine-layout" Class="almanac-layout" OnKeyDown="OnKeyDown">

        <HomeCard OnNav="OnNav" Model="Model" />
        <CatalogCard OnNav="OnNav" Model="Model" />
        <TopCard Model="Model" />
        <TimeLineCard />
        @if (Model.Entries.Any())
        {
            <GameListCard Model="Model" OnNav="OnNav" />
        }
        @if (Model.Articles.Any())
        {
            <InSideCard Model="Model" OnNav="OnNav" />
        }
        @if (Model.Entries.Any())
        {
            <NextCard Model="Model" OnNav="OnNav" />
        }
        <CnGalWebSite.Shared.MasaComponent.PC.Home.Almanac.Other.NavCard LayoutId="almanac-magazine-layout" @ref="navCard" />

    </CnGalWebSite.Shared.MasaComponent.Shared.Components.Magazine.MasaMagazineLayout>
}
else
{
    <CnGalWebSite.Components.Progressings.ProgressingCard Page />
}
@code {
    [Parameter]
    public string Id { get; set; }

    CnGalWebSite.Shared.MasaComponent.PC.Home.Almanac.Other.NavCard navCard;

    AlmanacViewModel Model { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            try
            {
                //获取主页信息
                Model = await _httpService.GetAsync<AlmanacViewModel>($"api/almanac/get?id={Id}");
                _eventService.OnTempEffectTheme(false, false, false, "light-red");
                NavigationManager.LocationChanged -= LocationChanged;
                NavigationManager.LocationChanged += LocationChanged;

                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取年鉴数据失败");
            }
        }
    }

    public void LocationChanged(object sender, LocationChangedEventArgs args)
    {
        _eventService.OnCleanTempEffectTheme();
    }

    public async Task OnNav(string id)
    {
        await navCard?.Nav(id);
    }

    public async Task OnKeyDown(KeyboardEventArgs e)
    {
        await navCard?.OnKeyDown(e);
    }
    public void Dispose()
    {

        NavigationManager.LocationChanged -= LocationChanged;
        GC.SuppressFinalize(this);
    }
}
